package influence.propagation.swing.implementation;

import influence.propagation.swing.implementation.Controller.ModelType;

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;

public class InfluencePropagation extends JFrame {
	
	private class MyWindowAdapter extends WindowAdapter{

		@Override
		public void windowClosed(WindowEvent e) {
			if(c!=null){
				c.closeDb();
			}
			ref.dispose();
		}
	}

	private static final long serialVersionUID = -2960731361476515165L;
	
	private Graph g;
	private Controller c;
	private InfluencePropagation ref;
	
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenu menu;
	private JMenuItem mntmCaricaGrafo;
	private JMenuItem mntmEsci;
	private JMenuItem mntmAbout;
	private JPanel panel;
	private JLabel lblComandi;
	private JButton btnDrawGraph;
	private JLabel lblIterazioni;
	private JButton btnNext;
	private JButton btnPrevious;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InfluencePropagation frame = new InfluencePropagation();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void instanziateController(File ... f){
		this.c = new Controller(ModelType.INDIPENDENT_CASCADE, f);
		btnDrawGraph.setEnabled(true);
		btnNext.setEnabled(false);
		btnPrevious.setEnabled(false);
	}
	
	public InfluencePropagation() {
		try {
			UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		setResizable(false);
		addWindowListener(new MyWindowAdapter());
		setTitle("Influence Propagation");
		setBounds(100, 100, 268, 260);
		setJMenuBar(getMenuBar_1());
		getContentPane().setLayout(new CardLayout(0, 0));
		getContentPane().add(getPanel(), "name_3930936401619");
		
		System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
		g = new SingleGraph("Influence Propagation");
		g.addAttribute("ui.stylesheet", "url('C:\\Users\\Valerio\\workspace\\InfluencePropagation\\src\\graphStyle.css')");
		g.addAttribute("ui.antialias");
		g.addAttribute("ui.quality");
//		Viewer viewer = new Viewer(g, ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
//		View view = viewer.addDefaultView(false);
//		panel_1.add(view);
		g.display();
		ref = this;
	}
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnFile());
			menuBar.add(getMenu());
		}
		return menuBar;
	}
	private JMenu getMnFile() {
		if (mnFile == null) {
			mnFile = new JMenu("File");
			mnFile.add(getMntmCaricaGrafo());
			mnFile.add(getMntmEsci());
		}
		return mnFile;
	}
	private JMenu getMenu() {
		if (menu == null) {
			menu = new JMenu("?");
			menu.add(getMntmAbout());
		}
		return menu;
	}
	private JMenuItem getMntmCaricaGrafo() {
		if (mntmCaricaGrafo == null) {
			mntmCaricaGrafo = new JMenuItem("Carica grafo");
			mntmCaricaGrafo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					GraphFileChooser gfc = new GraphFileChooser();
					gfc.setVisible(true);
					gfc.setParent(ref);
				}
			});
		}
		return mntmCaricaGrafo;
	}
	private JMenuItem getMntmEsci() {
		if (mntmEsci == null) {
			mntmEsci = new JMenuItem("Esci");
		}
		return mntmEsci;
	}
	private JMenuItem getMntmAbout() {
		if (mntmAbout == null) {
			mntmAbout = new JMenuItem("About");
		}
		return mntmAbout;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(null);
			panel.add(getLblComandi());
			panel.add(getBtnDrawGraph());
			panel.add(getLblIterazioni());
			panel.add(getBtnNext());
			panel.add(getBtnPrevious());
		}
		return panel;
	}
	private JLabel getLblComandi() {
		if (lblComandi == null) {
			lblComandi = new JLabel("Comandi");
			lblComandi.setBounds(6, 6, 55, 16);
		}
		return lblComandi;
	}
	private JButton getBtnDrawGraph() {
		if (btnDrawGraph == null) {
			btnDrawGraph = new JButton("Disegna grafo");
			btnDrawGraph.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					c.drawGraph(g);
					c.executeAlgoritm();
					btnDrawGraph.setEnabled(false);
					btnNext.setEnabled(true);
					btnPrevious.setEnabled(true);
				}
			});
			btnDrawGraph.setEnabled(false);
			btnDrawGraph.setBounds(6, 29, 136, 28);
		}
		return btnDrawGraph;
	}
	private JLabel getLblIterazioni() {
		if (lblIterazioni == null) {
			lblIterazioni = new JLabel("Iterazioni");
			lblIterazioni.setBounds(6, 69, 55, 16);
		}
		return lblIterazioni;
	}
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton("-->>");
			btnNext.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					c.stepNext(g);
				}
			});
			btnNext.setEnabled(false);
			btnNext.setBounds(6, 100, 90, 28);
		}
		return btnNext;
	}
	private JButton getBtnPrevious() {
		if (btnPrevious == null) {
			btnPrevious = new JButton("<<--");
			btnPrevious.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					c.stepBack(g);
				}
			});
			btnPrevious.setEnabled(false);
			btnPrevious.setBounds(6, 145, 90, 28);
		}
		return btnPrevious;
	}
}
